# encoding: utf-8
import uuid
import time
import requests
from utils.auth_head import gen_sign_headers
import numpy as np
from utils.knn_test import geteMbedding
# 请替换APP_ID、APP_KEY
APP_ID = '3033214471'
APP_KEY = 'BrSqHdybCJDPosUG'
DOMAIN = 'api-ai.vivo.com.cn'
METHOD = 'POST'
# url
ChatGpt_URI = '/vivogpt/completions'
Similarity_URI='/embedding-model-api/predict/batch'
# chatgpt
def sync_vivogpt(text="输出有什么可以帮助你的吗？"):
    params = {
        'requestId': str(uuid.uuid4())
    }
    print('requestId:', params['requestId'])

    data = {
        'prompt': text,
        'model': 'vivo-BlueLM-TB',
        'sessionId': str(uuid.uuid4()),
        'extra': {
            'temperature': 0.9
        }
    }
    headers = gen_sign_headers(APP_ID, APP_KEY, METHOD, ChatGpt_URI, params)
    headers['Content-Type'] = 'application/json'

    start_time = time.time()
    url = 'https://{}{}'.format(DOMAIN, ChatGpt_URI)
    response = requests.post(url, json=data, headers=headers, params=params)

    if response.status_code == 200:
        res_obj = response.json()
        print(f'response:{res_obj}')
        if res_obj['code'] == 0 and res_obj.get('data'):
            content = res_obj['data']['content']
            print(f'final content:\n{content}')
            return content;
    else:
        print(response.status_code, response.text)
    end_time = time.time()
    timecost = end_time - start_time
    print('请求耗时: %.2f秒' % timecost)
# 存入向量相似度
def embedding(text=[]):
    # ["腿部","身体","头部","健身操","舞蹈","跑步","手臂","腹部","胸部","臀部"]
    params = {}
    post_data = {
        "model_name": "m3e-base",
        "sentences": text
    }
    headers = gen_sign_headers(APP_ID, APP_KEY, METHOD, Similarity_URI, params)
    url = 'https://{}{}'.format(DOMAIN, Similarity_URI)
    response = requests.post(url, json=post_data, headers=headers)
    if response.status_code == 200:
    #存入.npy文件内部
        np.save('../web/data_message.npy', text)
        np.save('../web/data.npy', response.json()['data'])
        return "更新成功"
    else:
        print(response.status_code, response.text)
        return "更新失败"
# 比较向量相似度
def get_similarity(text=[]):
    params = {}
    post_data = {
        "model_name": "m3e-base",
        "sentences": text
    }
    headers = gen_sign_headers(APP_ID, APP_KEY, METHOD, Similarity_URI, params)
    url = 'https://{}{}'.format(DOMAIN, Similarity_URI)
    response = requests.post(url, json=post_data, headers=headers)
    if response.status_code == 200:
        return geteMbedding(response.json()['data'],text)
    else:
        print(response.status_code, response.text)

if __name__ == '__main__':
    list=get_similarity(text=["我想有腹肌，然后想变强壮"])
    print(list)